package com.tencent.compose.sample.mainpage.sectionItem

/*
 * Tencent is pleased to support the open source community by making ovCompose available.
 * Copyright (C) 2025 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import coil3.decode.DecodeResult
import coil3.decode.Decoder
import coil3.decode.ImageSource
import coil3.request.Options
import com.tencent.compose.GlobalDatabaseHolder
import com.tencent.compose.ListRepository
import com.tencent.compose.sample.IconOptionView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.withContext
import okio.use
import org.jetbrains.compose.resources.ExperimentalResourceApi
import kotlin.collections.get
//import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
//import androidx.compose.ui.Modifier
//import androidx.compose.ui.platform.LocalContext
import coil3.ImageLoader

@OptIn(ExperimentalResourceApi::class)
@Composable
internal fun RoomExamples() {
    val scope = rememberCoroutineScope()
    val listRepository = ListRepository(GlobalDatabaseHolder.database.getDao())
    val repos by listRepository.getKotlinTopRepos().collectAsState(initial = emptyList())
//    var name by remember { mutableStateOf("Button") }

    Column {
        Button(
            modifier = Modifier.fillMaxWidth().height(100.dp),
            onClick = {
                println("msyyyyy refresh")
                listRepository.getKotlinTopRepos()
            },
        ) {
        }

        LazyVerticalGrid(
            columns = GridCells.Fixed(5), // 一行五个 item
            contentPadding = PaddingValues(16.dp),
            verticalArrangement = Arrangement.spacedBy(2.dp),
            horizontalArrangement = Arrangement.spacedBy(2.dp)
        ) {
            items(repos.size) { index ->
                IconOptionView(repos[index].description, false, {})
            }
        }
    }

//    LazyColumn {
////        item {
////            Button(modifier = Modifier.weight(1f), onClick = {
////                name = getPlatform().name
////                scope.launch {
////                    println("pthead start")
////                    val count = listRepository.getKotlinTopRepos().last().size
////                    name = "${getPlatform().name} $count"
////                    println("pthead end")
////                }
////            }) {
////                Text(name)
////            }
////        }
//        items(repos.size) { index ->
//            Text(
//                modifier = Modifier.fillMaxWidth().padding(10.dp),
//                text = "${repos[index].id} ${repos[index].name} ${repos[index].stars} ${repos[index].description}"
//            )
//        }
//    }
}

